﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DTO;

namespace DAO
{
    public class TP_TRUNGGIAN_DAO
    {
        #region 1. Inserting
        public static bool ThemThanhPhoTrungGian(TP_TRUNGGIAN_DTO tptg)
        {
            bool result = false;
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTuyenDL", tptg.MaTuyenDL));
                sqlParams.Add(new SqlParameter("@MaTP", tptg.MaTP));
                //sqlParams.Add(new SqlParameter("@EmployeeStyle", emp.EmployeeStyle));
                // Call Store Procedure
                int n = SqlDataAccessHelper.ExecuteNoneQuery("InsertThanhPhoTrungGian", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 2. Updating
        public static bool SuaThanhPhoTrungGian(int MaTuyen,int MaTP1,int MaTP2)
        {
            bool result = false;
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTuyenDL",MaTuyen));
                sqlParams.Add(new SqlParameter("@MaTP1",MaTP1));
                sqlParams.Add(new SqlParameter("@MaTP2", MaTP2));


                // Call Store Procedure
                int n = SqlDataAccessHelper.ExecuteNoneQuery("UpdateThanhPhoTrungGian", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 3. Deleting
        public static bool XoaThanhPhoTrungGian(int maTuyen, int maTP)
        {
            bool result = false;
            try
            {
                // Create List Sql Parameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTuyenDL", maTuyen));
                sqlParams.Add(new SqlParameter("@MaTP", maTP));

                // Call Store Procedure
                int n = SqlDataAccessHelper.ExecuteNoneQuery("DeleteThanhPhoTrungGian", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion


        public static List<TP_TRUNGGIAN_DTO> SelectThanhPhoTrungGiansAll()
        {
            List<TP_TRUNGGIAN_DTO> list = new List<TP_TRUNGGIAN_DTO>();
            try
            {
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("SelectThanhPhoTrungGiansAll");
                foreach (DataRow dr in dt.Rows)
                {
                    TP_TRUNGGIAN_DTO tp = new TP_TRUNGGIAN_DTO();
                    tp.MaTuyenDL = (int)dr["MaTuyenDL"];
                    tp.MaTP = (int)dr["MaTP"];
                    list.Add(tp);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }
        public static List<TP_TRUNGGIAN_DTO> SelectThanhPhoTrungGiansByAndMaTuyen(int maTuyen)
        {
            List<TP_TRUNGGIAN_DTO> list = new List<TP_TRUNGGIAN_DTO>();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTuyenDL", maTuyen));
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("SelectThanhPhoTrungGianByAndMaTuyen", sqlParams);
                foreach (DataRow dr in dt.Rows)
                {
                    //DataRow dr = dt.Rows[0];                                   
                    TP_TRUNGGIAN_DTO tp = new TP_TRUNGGIAN_DTO();
                    tp.MaTuyenDL = (int)dr["MaTuyenDL"];
                    tp.MaTP = (int)dr["MaTP"];
                    list.Add(tp);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

    }

}
